package com.cungmua.app.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import com.cungmua.app.R;
import com.cungmua.app.adapter.ListPastDealAdapter;
import com.cungmua.app.base.BaseDrawerActivitys;
import com.cungmua.app.handler.PastDealHandler;
import com.cungmua.app.model.PastDeal;
import com.cungmua.app.model.request.CancelOrderRequest;
import com.cungmua.app.model.request.PastDealRequest;
import com.cungmua.app.model.response.CancelOrderResponse;
import com.cungmua.app.model.response.Pager;
import com.cungmua.app.model.response.PastDealResponse;
import com.cungmua.app.task.CancelOrderTask;
import com.cungmua.app.task.PastDealTask;
import com.cungmua.app.view.EndlessScrollListener;

import eu.erikw.PullToRefreshListView;

/**
 * Created by kelvin.nguyen on 6/25/14.
 */
public class PastDealsActivity extends BaseDrawerActivitys implements PastDealHandler {

    private PullToRefreshListView listDeal;
    private ListPastDealAdapter adapter;

    class EndlessDealListener extends EndlessScrollListener {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            if (totalItemsCount >= pager.getTotalRow()) {
                return;
            }
            currentPage = page;
            updateView();
        }
    }

    private EndlessScrollListener endlessScrollListener = new EndlessDealListener();
    private Pager pager;
    protected int currentPage = 1;
    @Override
    public int getLayoutResource() {
        return R.layout.activity_past_deals;
    }

    @Override
    public void updateView() {
        PastDealRequest request = new PastDealRequest(getDeviceId(), getToken());
        request.setCityId(getCurrentCity().getId());
        request.setUserId(getCurrentUser().getId());
        request.setPage(currentPage);

        PastDealTask task = new PastDealTask(this, this);
        task.execute(request);
    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        listDeal = (PullToRefreshListView) findViewById(R.id.listDeals);

        adapter = new ListPastDealAdapter(this,this);
        listDeal.setAdapter(adapter);
        listDeal.setDivider(null);
        listDeal.setDividerHeight(0);
        listDeal.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                updateView();
            }
        });
    }

    @Override
    public void onListPastDeal(PastDealResponse response) {
        listDeal.onRefreshComplete();
      //  listDeal.setEmptyView(findViewById(R.id.txtEmptyPastDeal));
        if (response.getData() != null) {
            if (response.getData().size() > 0) {
                if (currentPage == 1) {
                    adapter.setDataSource(response.getData());
                    pager = response.getPager();
                    endlessScrollListener = new EndlessDealListener();
                    listDeal.setOnScrollListener(endlessScrollListener);
                } else {
                    adapter.addToDataSource(response.getData());
                }
            } else {
                listDeal.setEmptyView(findViewById(R.id.txtEmptyPastDeal));
            }
        }
    }

    @Override
    public void onButtonDestroyClicked(final PastDeal pastDeal) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Bạn có chắc chắn muốn huỷ đơn hàng? (Trong trường hợp đơn hàng có nhiều sản phẩm, thì sẽ huỷ tất cả sản phẩm.)");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                CancelOrderRequest request = new CancelOrderRequest(getDeviceId(),getToken());
                request.setUserId(getCurrentUser().getId());
                request.setOrderId(pastDeal.getOrderId());
                request.setCityId(getCurrentCity().getId());
                CancelOrderTask task = new CancelOrderTask(PastDealsActivity.this,PastDealsActivity.this);
                task.execute(request);
            }
        });
        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();

    }

    @Override
    public void onCancelSuccess(CancelOrderResponse response) {
        if (response.getCode() == 0)
            updateView();
    }
}

